<?php
/**
	 * Solid Chat For Elgg
	 * 
	 * @package solidchat
	 * @author Arunoda Susiripala
	 * @copyright Arunoda Susiripala 2009
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @link http://elgg-solid-chat.googlecode.com
	 */

/**
 * Class that handles Chat Session between 2 or more users..
 * @todo curruntly only supports for 2 users .. need to extend it 
 *
 */
define("ELGG_SOLID_CHAT_SESSION","ELGG_SOLID_CHAT_SESSION");
define("ELGG_SOLID_CHAT_MESSAGE","ELGG_SOLID_CHAT_MESSAGE");

require_once("ChatMessage.php");

class ChatSession{
	public static 
		$IDLE=767,
		$LEAVE=768,
		$IN_CHAT=769,
		$OFFLINE=770;
		
	private $session;
	private $id;
	private $participants;
	/**
		@param $user1-guid of user1
		@param $user2-guid of user2
	 */
	public function __construct($user1,$user2){
		$key=($user1<$user2)?"$user1:$user2":"$user2:$user1";
		$this->id=$key;
		$this->participants=array($user1,$user2);
		//var_dump("solidchat".$this->id);
		$session_guid=datalist_get("solidchat{$this->id}");
		if($session_guid){
			$this->session=new ElggObject($session_guid);
		}
		else{
			$this->session=new ElggObject();
			$this->session->subtype =ELGG_SOLID_CHAT_SESSION;
			$this->session->access_id = ACCESS_LOGGED_IN;
			$this->session->set("participants",$this->participants);
			$this->session->save();
			$this->session->set("messageCount",0);
			
			datalist_set("solidchat{$this->id}",$this->session->guid);
		}
	}
	
	/**
		returns the ChatSession object from the given id
		@return ChatSession
	 */
	public static function instance($id){
		
	}
	
	/**
		@param $owner- guid of Owner of the message
		@param $message-the message text
	 */
	public function addMessage($owner,$message){
		if(!in_array($owner,$this->participants)) throw new Exception("
			message owner is not a particpant of this chat session");
		$this->session->annotate(ELGG_SOLID_CHAT_MESSAGE, $message, ACCESS_LOGGED_IN, $owner);
		$count=($this->session->get("messageCount"))?$this->session->get("messageCount"):0;
		$this->session->set("messageCount",$count+1);
		$this->session->save();
	}
	
	/**
		@param $owner - guid of action owner
		@param $action - the action...
	 */
	public function setAction($owner,$action){
		
	}
	
	/**
	 *  get the currunt action of participant..
		@param $onwer - action owner..
		@return the action stated as in static variables..
	 */
	public function getStatus($onwer){
		
	}
	
	/**
	 * 
	 * @return array of guids..
	 */
	public function getParticipants(){
		return $this->participants;
	}
	
	/**
	 * if $message is not provided get all the messages
	 * else 
	 *   messages >=messageID
		@param $messageID message id of the messages
		@return Message[]
	 */
	public function getMessages($messageID=null){
		
		$limit=$this->session->get("messageCount");
		$offset=($messageID)?$messageID:0;
		$annotates=$this->session->getAnnotations(
			ELGG_SOLID_CHAT_MESSAGE,$limit,$offset);
		
		$messages=array();
		foreach ($annotates as $id=>$val){
			$messages[]=new ChatMessage
				($offset+$id,$val->owner_guid,$val->value,$val->time_created);
			//$val->delete();
			//$this->session->set("messageCount",0);			
		}
		return $messages;
	}
	
	/**
		returns the message by given messageID..
		if $messageID is null return the last message
		
		@param $messageID message id
		@return Message
	 */
	public function getMessage($messageID=null){
		$count=$this->session->get("messageCount");
		$offset=(isset($messageID))?$messageID:$count-1;
		$annotates=$this->session->getAnnotations(
			ELGG_SOLID_CHAT_MESSAGE,1,$offset);
		//var_dump($annotates[0]);
		$val=$annotates[0];
		if(!$val->time_created) return null;
		return new ChatMessage
				($offset,$val->owner_guid,$val->value,$val->time_created);
	}
	
	/**
	 * delete the current object
	 * Enter description here...
	 * @return unknown_type
	 */
	public function delete(){
		datalist_set("solidchat{$this->id}",0);
		$this->session->delete();
	}
	
	/**
		if $preserve not given..
		whole the messages will be deleted;
		else amount of $preserve messages will be stayed.. (from descending timestamp)
	 	@param $preserve no of messages to be preserved
	 */
	public function refresh($preserve=null){
		
	}
	
	/**
	 * @return int
	 */
	public function messageCount(){
		return ($this->session->get("messageCount"))?$this->session->get("messageCount"):0;
	}
	
	/** 
	 * 
	 * returns a unique id for this chat_session...
	 * @return unknown_type
	 */
	public function getID(){
		return $this->id;
	}
}